package view
{
	import alternativa.engine3d.events.MouseEvent3D;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.primitives.Box;
	import alternativa.types.Point3D;
	
	import com.adobe.cairngorm.control.CairngormEvent;

	public class Cube extends Box
	{
		public var id:int;
		
		public function Cube(width:Number=10, length:Number=10, height:Number=10, widthSegments:uint=1, lengthSegments:uint=1, heightSegments:uint=1, reverse:Boolean=false, triangulate:Boolean=false)
		{
			super(width, length, height, widthSegments, lengthSegments, heightSegments, reverse, triangulate);
			init();
		}
		
		private function init():void
		{	
			this.addEventListener(MouseEvent3D.MOUSE_OUT, onOut);
			this.getSurfaceById("top").addEventListener(MouseEvent3D.MOUSE_OVER, onOver);
			this.getSurfaceById("bottom").addEventListener(MouseEvent3D.MOUSE_OVER, onOver);
			this.getSurfaceById("right").addEventListener(MouseEvent3D.MOUSE_OVER, onOver);
			this.getSurfaceById("left").addEventListener(MouseEvent3D.MOUSE_OVER, onOver);
			this.getSurfaceById("front").addEventListener(MouseEvent3D.MOUSE_OVER, onOver);
			this.getSurfaceById("back").addEventListener(MouseEvent3D.MOUSE_OVER, onOver);
			onOut(null);
		}
		
		private function onClick(e:MouseEvent3D):void
		{
			
		}
		
		private function onOut(e:MouseEvent3D):void
		{
			this.setMaterialToSurface(new FillMaterial(0xFF0000), "top");
			this.setMaterialToSurface(new FillMaterial(0xFF00FF), "bottom");
			this.setMaterialToSurface(new FillMaterial(0xFFFF00), "left");
			this.setMaterialToSurface(new FillMaterial(0x0000FF), "right");
			this.setMaterialToSurface(new FillMaterial(0x00FFFF), "front");
			this.setMaterialToSurface(new FillMaterial(0xFFFFFF), "back");
		}
		
		private function onOver(e:MouseEvent3D):void
		{
			onOut(e)
			this.setMaterialToSurface(new FillMaterial(0xFFFFFF,0.5), e.surface);
			var event:CairngormEvent = new CairngormEvent("console");
			//Alert.show(e.currentTarget.mesh.transformation.getRotations())
			var rotations:Point3D = new Point3D();
			switch(e.surface.id)
			{
				case "front" :
					rotations.x = 2;
				break;
				case "back" :
					rotations.x = 1;
				break;
				case "" :
				break;
				case "" :
				break;
			}
			

			
			var object:Object = { cube:this, surface:e.surface.id, rotation:rotations };
			event.data = object;
			event.dispatch();	
		}
	}
}